package DMSXL._2023Before.String_;

/*public class reverseWords_ {
    public static void main(String[] args) {
        String s = "a good   example";
        Solution solution = new Solution();
        System.out.println(solution.reverseWords(s));
    }
}
class Solution {
    public String reverseWords(String s) {
        s = s.trim();//去除首尾空格
        System.out.println(s);
        char[] ch = s.toCharArray();
        char[] newch = new char[ch.length];
        int index = 0;
        int start = s.length() - 1;
        int end = s.length() - 1;
        for (int i = s.length() - 1; i >= 0 ; i--) {
            if(ch[i] == ' ')
                continue;
            else{
                for (int j = i; j >= 0 ; j--) {
                    if(ch[j] == ' '){

                        add(newch,ch,j+1,i,index);
                        index += i - j + 1 ;
                        i = j;
                        break;
                    }
                    else if(j == 0){

                        add(newch,ch,j,i,index);
                        index += i - j + 1;
                        i = j;
                        break;
                    }
                }
            }
        }

        return String.valueOf(newch).trim();
    }

    public void add(char[] newch,char[] ch,int start ,int end,int index){
        int k = end - start ;
        int starts = start;
        for (int i = 0; i <= k; i++) {
            newch[index++] = ch[start++];
        }

        if(starts != 0){
            newch[index++] = ' ';
        }
    }

}
*/